749C - Voting - CodeForces Solution


greedy implementation two pointers *1500

Please click on ads to support us..

Python Code:

from collections import deque
n=int(input())
data,d1,d2=input(),deque(),deque()
for i in range(n):
    if data[i]=="R":
        d1.append(i)
    else:
        d2.append(i)
while True:
    try:
        if d1[0] > d2[0]:
            d1.popleft()
            d2.append(n+d2[0])
            d2.popleft()
        else:
            d2.popleft()
            d1.append(n+d2[0])
            d1.popleft()
    except IndexError:
        break
if d1:
    print('R')
else:
    print('D')

C++ Code:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int num;
    cin>>num;
        string s;
        cin>>s;
        
        queue<int>d,r;
        for(int i=0;i<s.size();i++)
        {
            s[i]=='D'?d.push(i):r.push(i);
        }
        int n=s.size();
        int curr=n;
        while(!d.empty() && !r.empty())
        {
            int d1=d.front();
            int r1=r.front();
            d.pop();
            r.pop();
            d1<r1?  d.push(curr++) :  r.push(curr++);
        }
        if(d.size())
        {
            cout<<"D";
           
        }
        else
        {
             cout<<"R";
        }
           
   
}


Comments

Submit
0 Comments
More Questions

988B - Substrings Sort
312A - Whose sentence is it
513A - Game
1711E - XOR Triangle
688A - Opponents
20C - Dijkstra
1627D - Not Adding
893B - Beautiful Divisors
864B - Polycarp and Letters
1088A - Ehab and another construction problem
1177B - Digits Sequence (Hard Edition)
1155B - Game with Telephone Numbers
1284A - New Year and Naming
863B - Kayaking
1395B - Boboniu Plays Chess
1475D - Cleaning the Phone
617B - Chocolate
1051B - Relatively Prime Pairs
95B - Lucky Numbers
1692D - The Clock
1553D - Backspace
1670D - Very Suspicious
1141B - Maximal Continuous Rest
1341A - Nastya and Rice
1133A - Middle of the Contest
385A - Bear and Raspberry
1311B - WeirdSort
1713F - Lost Array
236B - Easy Number Challenge
275A - Lights Out